package client.comm.baoding.ui

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import client.comm.commlib.dialog.ValidatePwdDialog
import client.comm.baoding.R
import client.comm.baoding.databinding.ActivityQbyeBinding
import client.comm.baoding.databinding.LayoutWalletdetailitemBinding
import client.comm.baoding.ui.vm.QbyeViewModel
import java.util.*
import client.comm.baoding.BaseMiddleActivity

class QbyeActivity : BaseMiddleActivity<QbyeViewModel, ActivityQbyeBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_qbye
    }

    var timerTask: TimerTask? = null
    var timer: Timer? = null
    override fun initView() {
        binding?.apply {
            event = this@QbyeActivity
            vm = viewModel
            emptyLayout.setOnClickListener {
                viewModel.pageOne.value = 0
                viewModel.holdOne()
            }
        }
    }

    override fun initData() {
        viewModel.holdOne()
    }

    override fun observerUI() {
        viewModel.callOne.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    it.data?.record_list?.let {
                        this@QbyeActivity.binding.itemWrap.removeAllViews()

                        if (it.size > 0) {
                            binding.zwView.visibility = View.GONE
                            for (e in it) {
                                DataBindingUtil.inflate<LayoutWalletdetailitemBinding>(
                                    LayoutInflater.from(
                                        this
                                    ),
                                    R.layout.layout_walletdetailitem,
                                    this@QbyeActivity.binding.itemWrap,
                                    false
                                ).apply {
                                    bean = e
                                    when (e.r_state) {
                                        1 -> {
                                            num = "+${e.num}"
                                        }
                                        2 -> {
                                            num = "-${e.num}"
                                        }
                                    }
                                    this@QbyeActivity.binding.itemWrap.addView(root)
                                }
                            }
                        } else {
                            binding.zwView.visibility = View.VISIBLE
                        }
                    }

                }
            }
        })
    }

    override fun initViewModel(): QbyeViewModel = createViewModel()
    override fun onDestroy() {
        timerTask?.let { it.cancel() }
        timer?.let { it.cancel() }
        super.onDestroy()
    }

    fun ok() {
        ValidatePwdDialog(this).apply {
            title = "需要验证支付密码"
            ok = {
                viewModel.ok(it)
            }
        }.show()
    }

    fun jl() {
        startActivity(Intent(this, WalletDetailActivity::class.java))
    }

}